<template>
  <div
    style="border:1px solid #cccccc; border-radius: 8px; width: 500px; height: 430px; background-color: #d6eff7"
    v-loading="loading"
  >
    <h1>用户注册</h1>

    <el-form :model="user" :rules="rules" ref="user" label-width="180px">
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="user.userName"></el-input>
      </el-form-item>

      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input v-model="user.phoneNumber"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="user.password"></el-input>
      </el-form-item>

      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="user.email"></el-input>
      </el-form-item>

      <div style="height: 20px;"></div>

      <el-button type="primary" plain @click="change()">返回登录页面</el-button>
      <el-button @click="resetForm('user')">重置</el-button>
      <el-button type="primary" @click="submitForm('user')">注册</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      loading: false,
      user: {
        userName: "",
        phoneNumber: "",
        password: "",
        email: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { max: 20, message: "用户名过长", trigger: "blur" },
        ],
        phoneNumber: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          { pattern: /^[0-9]+$/, message: "只能输入数字" },
          { min: 11, max: 11, message: "长度错误", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { pattern: /^(\S)*$/, message: "不能输入空格" },
        ],
        email: [
          { pattern: /^(\w)+@(\S)+$/, message: "非法邮箱", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    change() {
      this.$emit("toLogin");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
          var url = process.env.URL_HEAD + "/touser/user/user/register";

          this.loading = true;
          this.$http.post(url, this.user).then(
            (response) => {
              // console.log(response);

              if (response.data.status == "ok") {
                this.$message.success("注册成功！");

                this.$refs["user"].resetFields();
                this.change();
              } else {
                this.$message.error("注册失败 " + response.data.message);
              }

              this.loading = false;
            },
            (response) => {
              this.$message.error("系统异常，请重试");
              
              this.loading = false;
            }
          );
        } else {
          this.$message.error("请输入完整信息");
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.h1 {
  font-size: 50px;
}
.el-input {
  width: 200px;
}
</style>
